﻿using DevExpress.XtraEditors;
using System;
using System.Linq;
using System.Windows.Forms;

namespace Inaction.UI.Settings
{
    public static class DateEditSetting
    {
        public static void SetProperties(DateEdit edit)
        {
            if (edit.DataBindings.Count > 0)
            {
                var binding = edit.DataBindings.Cast<Binding>().FirstOrDefault(p => p.PropertyName == "EditValue");
                if (binding.BindingMemberInfo.BindingField.EndsWith("Time", StringComparison.OrdinalIgnoreCase))
                {
                    edit.Properties.EditMask = "G";
                    edit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    edit.Properties.DisplayFormat.FormatString = "G";
                    edit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    edit.Properties.EditFormat.FormatString = "G";
                }
                if (binding.BindingMemberInfo.BindingField.EndsWith("Date", StringComparison.OrdinalIgnoreCase))
                {
                    edit.Properties.EditMask = "D";
                    edit.Properties.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    edit.Properties.DisplayFormat.FormatString = "D";
                    edit.Properties.EditFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    edit.Properties.EditFormat.FormatString = "D";
                }
            }
        }
    }
}
